#pragma once
#include "RBTree.hpp"

namespace lw
{
	template<class K, class V>
	class map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<const K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::iterator iterator;
		iterator begin()
		{
			return _t.begin();
		}
		iterator end()
		{
			return _t.end();
		}
		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _t.Insert(kv);
		}
		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert(make_pair(key, V()));
			return ret.first->second;
		}
	private:
		RBTree<K, pair<const K, V>, MapKeyOfT> _t;
	};
	void TestMap1()
	{
		map<int, int> m;
		int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
		for (auto e : a)
			m.insert({ e, e });
		map<int, int>::iterator it = m.begin();
		while (it != m.end())
		{
			cout << it->first << " : " << it->second << endl;
			++it;
		}
	}
}